package com.macro.mall.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsExhibitionsActivitiesGoodsParam;
import com.macro.mall.mapper.BmsExhibitionsActivitiesGoodsMapper;
import com.macro.mall.mapper.BmsExhibitionsActivitiesGoodsPicMapper;
import com.macro.mall.model.BmsExhibitionsActivitiesGoods;
import com.macro.mall.model.BmsExhibitionsActivitiesGoodsPic;
import com.macro.mall.service.BmsExhibitionsActivitiesGoodsService;
import com.macro.mall.utils.UserUtils;


/**
 * 观光车点位图片Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsExhibitionsActivitiesGoodsServiceImpl implements BmsExhibitionsActivitiesGoodsService {
    @Autowired
    private BmsExhibitionsActivitiesGoodsMapper bmsExhibitionsActivitiesGoodsMapper;
    @Autowired
    private UserUtils userUtils;

    @Autowired
    private BmsExhibitionsActivitiesGoodsPicMapper bmsExhibitionsActivitiesGoodsPicMapper;


    /**
     * 分页查询展览活动
     * @return
     */
    @Transactional
    @Override
    public List<BmsExhibitionsActivitiesGoods> getList(BmsExhibitionsActivitiesGoodsParam bmsExhibitionsActivitiesGoodsParam) {
        PageHelper.startPage(bmsExhibitionsActivitiesGoodsParam.getPageNum(), bmsExhibitionsActivitiesGoodsParam.getPageSize());;
        BmsExhibitionsActivitiesGoods bmsExhibitionsActivitiesGoods = new BmsExhibitionsActivitiesGoods();
        BeanUtils.copyProperties(bmsExhibitionsActivitiesGoodsParam, bmsExhibitionsActivitiesGoods);
        List<BmsExhibitionsActivitiesGoods> list = bmsExhibitionsActivitiesGoodsMapper.selectAll(bmsExhibitionsActivitiesGoods);
        return list;
    }

    /**
     * 新增展览活动
     * @param
     * @return
     */
    @Transactional
    @Override
    public int create(BmsExhibitionsActivitiesGoodsParam bmsExhibitionsActivitiesGoodsParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsExhibitionsActivitiesGoods bmsExhibitionsActivitiesGoods = new BmsExhibitionsActivitiesGoods();
        BeanUtils.copyProperties(bmsExhibitionsActivitiesGoodsParam, bmsExhibitionsActivitiesGoods);
        bmsExhibitionsActivitiesGoods.setCreateUser(userId);
        bmsExhibitionsActivitiesGoods.setCreateTime(new Date());
        bmsExhibitionsActivitiesGoods.setUpdateUser(userId);
        bmsExhibitionsActivitiesGoods.setUpdateTime(new Date());
        int count = bmsExhibitionsActivitiesGoodsMapper.insert(bmsExhibitionsActivitiesGoods);
        //新增展品表数据之后，新增展品图片表
        if (count > 0){
            List<BmsExhibitionsActivitiesGoodsPic> bmsExhibitionsActivitiesGoodsPicList = new ArrayList<BmsExhibitionsActivitiesGoodsPic>();
            for (String pic : bmsExhibitionsActivitiesGoodsParam.getPicList()) {
                BmsExhibitionsActivitiesGoodsPic bmsExhibitionsActivitiesGoodsPic = new BmsExhibitionsActivitiesGoodsPic();
                //展览活动id
                bmsExhibitionsActivitiesGoodsPic.setExhibitionsActivitiesGoodsId(bmsExhibitionsActivitiesGoods.getId());
                //图片地址
                bmsExhibitionsActivitiesGoodsPic.setPic(pic);
                bmsExhibitionsActivitiesGoodsPic.setIsCover(0);
                bmsExhibitionsActivitiesGoodsPicList.add(bmsExhibitionsActivitiesGoodsPic);
            }
            if (StringUtils.isNotBlank(bmsExhibitionsActivitiesGoodsParam.getCoverPic())) {
                BmsExhibitionsActivitiesGoodsPic bmsExhibitionsActivitiesGoodsPic = new BmsExhibitionsActivitiesGoodsPic();
                //展览活动id
                bmsExhibitionsActivitiesGoodsPic.setExhibitionsActivitiesGoodsId(bmsExhibitionsActivitiesGoods.getId());
                //图片地址
                bmsExhibitionsActivitiesGoodsPic.setPic(bmsExhibitionsActivitiesGoodsParam.getCoverPic());
                bmsExhibitionsActivitiesGoodsPic.setIsCover(1);
                bmsExhibitionsActivitiesGoodsPicList.add(bmsExhibitionsActivitiesGoodsPic);
            }
            for (BmsExhibitionsActivitiesGoodsPic bmsExhibitionsActivitiesGoodsPic : bmsExhibitionsActivitiesGoodsPicList) {
                bmsExhibitionsActivitiesGoodsPicMapper.insert(bmsExhibitionsActivitiesGoodsPic);
            }
        }
        return count;
    }

    /**
     * 更新展览活动
     * @param
     * @return
     */
    @Transactional
    @Override
    public int update(BmsExhibitionsActivitiesGoodsParam bmsExhibitionsActivitiesGoodsParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsExhibitionsActivitiesGoods bmsExhibitionsActivitiesGoods = new BmsExhibitionsActivitiesGoods();
        BeanUtils.copyProperties(bmsExhibitionsActivitiesGoodsParam, bmsExhibitionsActivitiesGoods);
        bmsExhibitionsActivitiesGoods.setUpdateUser(userId);
        bmsExhibitionsActivitiesGoods.setUpdateTime(new Date());
        //更新展览活动
        int count = bmsExhibitionsActivitiesGoodsMapper.update(bmsExhibitionsActivitiesGoods);
        if (count > 0){
            //删除展览活动展品图片
            List<Long> id = new ArrayList<>();
            id.add(bmsExhibitionsActivitiesGoodsParam.getId());
            bmsExhibitionsActivitiesGoodsPicMapper.delete(id);
            List<BmsExhibitionsActivitiesGoodsPic> bmsExhibitionsActivitiesGoodsPicList = new ArrayList<BmsExhibitionsActivitiesGoodsPic>();
            for (String pic : bmsExhibitionsActivitiesGoodsParam.getPicList()) {
                BmsExhibitionsActivitiesGoodsPic bmsExhibitionsActivitiesGoodsPic = new BmsExhibitionsActivitiesGoodsPic();
                //展览活动id
                bmsExhibitionsActivitiesGoodsPic.setExhibitionsActivitiesGoodsId(bmsExhibitionsActivitiesGoods.getId());
                //图片地址
                bmsExhibitionsActivitiesGoodsPic.setPic(pic);
                bmsExhibitionsActivitiesGoodsPic.setIsCover(0);
                bmsExhibitionsActivitiesGoodsPicList.add(bmsExhibitionsActivitiesGoodsPic);
            }
            if (StringUtils.isNotBlank(bmsExhibitionsActivitiesGoodsParam.getCoverPic())) {
                BmsExhibitionsActivitiesGoodsPic bmsExhibitionsActivitiesGoodsPic = new BmsExhibitionsActivitiesGoodsPic();
                //展览活动id
                bmsExhibitionsActivitiesGoodsPic.setExhibitionsActivitiesGoodsId(bmsExhibitionsActivitiesGoods.getId());
                //图片地址
                bmsExhibitionsActivitiesGoodsPic.setPic(bmsExhibitionsActivitiesGoodsParam.getCoverPic());
                bmsExhibitionsActivitiesGoodsPic.setIsCover(1);
                bmsExhibitionsActivitiesGoodsPicList.add(bmsExhibitionsActivitiesGoodsPic);
            }
            for (BmsExhibitionsActivitiesGoodsPic bmsExhibitionsActivitiesGoodsPic : bmsExhibitionsActivitiesGoodsPicList) {
                bmsExhibitionsActivitiesGoodsPicMapper.insert(bmsExhibitionsActivitiesGoodsPic);
            }
        }
        return count;
    }

    /**
     * 获取展览活动信息
     * @param id
     * @return
     */
    @Override
    public BmsExhibitionsActivitiesGoods getItem(Long id) {
        BmsExhibitionsActivitiesGoods entity = bmsExhibitionsActivitiesGoodsMapper.selectById(id);
        if (entity == null) {
            return new BmsExhibitionsActivitiesGoods();
        }
        List<String> picList = bmsExhibitionsActivitiesGoodsPicMapper.findPicListByExhibitionsActivitiesGoodsId(entity.getId());
        entity.setPicList(picList);
        return entity;
    }

    /**
     * 删除展览活动
     * @param ids
     * @return
     * 完成
     */
    @Transactional
    @Override
    public int delete(List<Long> ids) {
        return bmsExhibitionsActivitiesGoodsMapper.delete(ids);
    }

}
